/**
 * File: AbstractSequenceProcessor.java
 * Created by: mhaimel
 * Created on: 22 Jul 2009
 * CVS:  $Id: AbstractSequenceProcessor.java,v 1.2 2009/08/13 10:40:52 mhaimel Exp $
 */
package uk.ac.ebi.curtain.processor.writer.impl;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import uk.ac.ebi.curtain.configuration.CurtainInputArguments;
import uk.ac.ebi.curtain.processor.AbstractProcessor;
import uk.ac.ebi.curtain.util.impl.MultiFileFastqWriter;
import uk.ac.ebi.curtain.utils.data.FileType;
import uk.ac.ebi.curtain.utils.data.ReadType;
import uk.ac.ebi.curtain.utils.file.FileInfo;
import uk.ac.ebi.velvet.model.Supercontig;

/**
 * @author mhaimel
 *
 */
public abstract class AbstractSequenceProcessor<I extends Serializable,O extends Serializable> extends AbstractProcessor<I,O> {
	private MultiFileFastqWriter writer; 
	private Map<FileInfo,Integer> outFileMap = new HashMap<FileInfo, Integer>();
	
	protected MultiFileFastqWriter getWriter(){
		if(null == writer){
			writer = _newWriter();
		}
		return writer;
	}

	public void reset() {
		writer = null;
		outFileMap = new HashMap<FileInfo, Integer>();
	}
	
	protected MultiFileFastqWriter _newWriter() {
		return new MultiFileFastqWriter(getContext().getFileAccess());
	}
	
	protected Map<FileInfo, Integer> getOutFiles() {
		return outFileMap;
	}
	
	protected void _prepareWriter(File baseDir) {
		CurtainInputArguments args = getContext().getArguments();
		for(FileInfo paired : args.getFiles(ReadType.ShortPaired, FileType.getSequenceTypes())){
			Integer fId = getContext().getGlobalFileIndex().getId(paired);
			Integer cat = args.getCategory(paired);
			FileInfo outFile = Supercontig.getPairedReadOutputFile(baseDir, cat);
			getWriter().addOutputFile(cat, outFile);
			getWriter().addFileId(fId, paired);
			getWriter().addFile(paired, cat);

			outFileMap.put(outFile,cat);
		}
		for(FileInfo read : args.getFiles(ReadType.Short, FileType.getSequenceTypes())){
			Integer fId = getContext().getGlobalFileIndex().getId(read);
			Integer cat = args.getCategory(read);
			FileInfo outFile = Supercontig.getReadOutputFile(baseDir);
			getWriter().addOutputFile(cat, outFile);
			getWriter().addFileId(fId, read);
			getWriter().addFile(read, cat);

			outFileMap.put(outFile,cat);
		}
		/* need to write contig files as well to load Ids for contigs and for checks */
		for(FileInfo read : args.getFiles(ReadType.Contig, FileType.getSequenceTypes())){
			Integer fId = getContext().getGlobalFileIndex().getId(read);
			Integer cat = args.getCategory(read);
			FileInfo outFile = Supercontig.getContigDumpFile(baseDir);
			getWriter().addOutputFile(cat, outFile);
			getWriter().addFileId(fId, read);
			getWriter().addFile(read, cat);

			outFileMap.put(outFile,cat);
		}
	}

	
}